//
//  TPPicker_date.m
//  TPPickerDemo
//
//  Created by 谭鹏 on 2017/8/5.
//  Copyright © 2017年 谭鹏. All rights reserved.
//

#import "TPPicker_date.h"
#import <NSDateFormatter+TP_Utility.h>
//typedef NS_ENUM(NSInteger,TPPicker_date_type) {
//    TPPicker_date_type_y = 1<<0,
//    TPPicker_date_type_M = 1<<1,
//    TPPicker_date_type_d = 1<<2,
//    TPPicker_date_type_h = 1<<3,
//    TPPicker_date_type_m = 1<<4,
//    TPPicker_date_type_s = 1<<5,
//};
@interface TPPicker_date()<UIPickerViewDelegate,UIPickerViewDataSource>
@property NSCalendarUnit picker_date_type;

@property NSDate * startDate;
@property NSDate * endDate;
@property NSDate * selectedDate;
@property NSMutableArray <NSNumber *>* dateTypes;

@property UIView * selectedBar;
@end
@implementation TPPicker_date
//年月日时分秒 1<<2 ~ 1<<7,必须单位连贯
- (instancetype)init
{
    self = [super init];
    if (self) {
        
        [self setPickerView];
        _dateTypes = [NSMutableArray new];
        _selectedDate = [NSDate new];
    }
    return self;
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    //隐藏 2根 线
    UIView * topLineView = [_pickerView valueForKey:@"topLineView"];
    UIView * bottomLineView = [_pickerView valueForKey:@"bottomLineView"];
    [topLineView setHidden:YES];
    [bottomLineView setHidden:YES];
}
- (void)setPickerView{
    _pickerView = [UIPickerView new];
    
    
    _pickerView.delegate = self;
    _pickerView.dataSource = self;
    
    [self.contentContainer addSubview:_pickerView];
    
    [_pickerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(self.contentContainer);
    }];
    
    _selectedBar = [UIView new];
    _selectedBar.userInteractionEnabled = NO;
    [self addSubview:_selectedBar];
    
        _selectedBar.layer.borderColor = [UIColor blackColor].CGColor;
        _selectedBar.layer.cornerRadius = 5;
        _selectedBar.layer.borderWidth = 1;
        
        [_selectedBar mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerY.mas_equalTo(self.contentContainer);
            make.height.mas_equalTo(35);
            
        }];
    
//    [_pickerView reloadAllComponents];
//    [_pickerView layoutIfNeeded];
//    UIView * topLineView = [_pickerView valueForKey:@"topLineView"];
//    UIView * bottomLineView = [_pickerView valueForKey:@"bottomLineView"];
//    [topLineView setHidden:YES];
//    [bottomLineView setHidden:YES];
    
}
-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    
    return _dateTypes.count ;
}
-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    NSCalendarUnit unit = _dateTypes[component].unsignedIntegerValue;
    return [[NSCalendar currentCalendar] rangeOfUnit:unit inUnit:unit>>1 forDate:_selectedDate].length;
    
}

-(CGFloat)pickerView:(UIPickerView *)pickerView widthForComponent:(NSInteger)component{
    return UIScreen.mainScreen.bounds.size.width/_dateTypes.count;
}
-(CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    return 35;
}
-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
    NSCalendarUnit unit = _dateTypes[component].unsignedIntegerValue;
    NSString * unitStr = @"";
    switch (unit) {
        case NSCalendarUnitYear:
            unitStr = @"年";
            break;
        case NSCalendarUnitMonth:
            unitStr = @"月";
            break;
        case NSCalendarUnitDay:
            unitStr = @"日";
            break;
        case NSCalendarUnitHour:
            unitStr = @"时";
            break;
        case NSCalendarUnitMinute:
            unitStr = @"分";
            break;
        case NSCalendarUnitSecond:
            unitStr = @"秒";
            break;
            
        default:
            break;
    }
    
    return [NSString stringWithFormat:@"%li%@",(long)row+1,unitStr];
}
-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{

    NSCalendarUnit unit = _dateTypes[component].unsignedIntegerValue;
    
#warning to fix
    NSCalendar * calender = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
    
    NSDateComponents *comps = [calender components:NSCalendarUnitYear|NSCalendarUnitMonth|NSCalendarUnitDay|NSCalendarUnitHour|NSCalendarUnitMinute|NSCalendarUnitSecond fromDate:_selectedDate];
    [comps setCalendar:calender];//没这部  comps.date 为nnull
    [comps setValue:(row+1)   forComponent:unit];
    
//    NSLog(@"selecteddate %@ datNSDate * date = [comps date];
    [self updateSelectedDate:[comps date]];
    //处理提示 选中时间
    NSArray * strs = @[@"",@"",@"年",@"月",@"日",@"时",@"分",@"秒"];
    NSMutableString * tipStr = [NSMutableString new];
    for (int i = 0; i <= component; i ++) {
        NSCalendarUnit subUnit= _dateTypes[i].unsignedIntegerValue;
        
        
        
        [tipStr appendFormat:@"%li%@",[comps valueForComponent:subUnit] ,strs[(int)sqrt(subUnit)]];
        
    }
    
    self.label.text = tipStr;
    
    //选中框 位移
    CGFloat leftspace = component*(UIScreen.mainScreen.bounds.size.width)/(_dateTypes.count);
    CGFloat rightspace = (component+1)*(UIScreen.mainScreen.bounds.size.width)/(_dateTypes.count);
    [_selectedBar mas_updateConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.contentContainer).offset(leftspace);
        make.right.mas_equalTo(self.contentContainer.mas_left).offset(rightspace);
    }];
    
}
// 此方法的优先级高于上面的方法,实现了此方法,则上面的方法就不执行了,这个很好理解
//// 经过我的测试,此方法的reusingView并没什么用,可能是iOS7之后不再使用,方法说明中关于此参数是这样说的"A view object that was previously used for this row, but is now hidden and cached by the picker view.",另外参考http://stackoverflow.com/questions/20635949/reusing-view-in-uipickerview-with-ios-7
//- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(nullable UIView *)view  {
//    
//    UILabel * label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 100, 40)];
//    
//    if (component == 0) {
//        
//        label.text = self.m_yearArray[row];
//        
//    } else {
//        
//        label.text = self.m_monthArray[row];
//    }
//    
//    
//    return label;
//}

- (void)setDateType:(NSCalendarUnit)type{
    _picker_date_type  = type;
    _dateTypes = [NSMutableArray new];
    for (int i = 0; i < 6; i ++) {
        NSUInteger unit = NSCalendarUnitYear <<i;
        if (_picker_date_type & unit) {
            [_dateTypes addObject:[NSNumber numberWithUnsignedInteger:unit]];
        }
    }
}
- (void)updateSelectedDate:(NSDate*)date{
    _selectedDate = date;
    for (int i = 0;  i <_dateTypes.count; i ++) {
        NSCalendarUnit unit = _dateTypes[i].unsignedIntegerValue;
        NSInteger row = [[NSCalendar currentCalendar] component:unit fromDate:date];
        [_pickerView selectRow:row-1 inComponent:i animated:NO];
        [_pickerView reloadAllComponents];
    }
    
}
@end
